package com.lvmama.task.common.utils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

@XmlRootElement(name = "root")
public class TaskXmlRoot implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5214172804666487761L;
	private List<TaskXmlModel> tasks;

	@XmlElementWrapper(name = "tasks")
	@XmlElement(name = "task")
	public List<TaskXmlModel> getTasks() {
		return tasks;
	}

	public void setTasks(List<TaskXmlModel> tasks) {
		this.tasks = tasks;
	}

	public void response(OutputStream os) throws Exception {
		JAXBContext context = JAXBContext.newInstance(TaskXmlRoot.class);
		Marshaller marshaller = context.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
		marshaller.marshal(this, os);
	}
	
	public static TaskXmlRoot getInstance(InputStream is) throws Exception {
		JAXBContext context = JAXBContext.newInstance(TaskXmlRoot.class);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		return (TaskXmlRoot) unmarshaller.unmarshal(is);
	}
}
